<template>
  <view class="container">
    <!-- 表单组件 -->
    <view class="form-wrapper">
      <u-form :model="form" ref="uForm" label-width="140rpx">
        <u-form-item label="店铺名称" prop="store_name">
          <u-input v-model="form.store_name" placeholder="请输入店铺名称" />
        </u-form-item>
        <u-form-item label="联系人" prop="contact">
          <u-input v-model="form.contact" placeholder="请输入联系人" />
        </u-form-item>
        <u-form-item label="联系电话" prop="phone">
          <u-input v-model="form.phone" placeholder="请输入联系电话" />
        </u-form-item>
        <u-form-item label="营业状态" prop="status">
		  <u-radio-group v-model="form.status">
			<u-radio name="1">营业中</u-radio>
			<u-radio name="0">已打样</u-radio>
		  </u-radio-group>
        </u-form-item>
        <u-form-item label="详细地址" prop="address" :border-bottom="false">
          <u-input v-model="form.address" placeholder="街道门牌、楼层等信息" />
        </u-form-item>
      </u-form>
    </view>
    <!-- 操作按钮 -->
    <view class="footer">
      <view class="btn-wrapper">
        <view class="btn-item btn-item-main" :class="{ disabled }" @click="handleSubmit()">保存</view>
      </view>
    </view>
  </view>
</template>

<script>
  // import SelectRegion from '@/components/select-region/select-region'
  import { isMobile } from '@/utils-shop/verify'
  import * as SettingApi from '@/apishop/setting'

  // 表单验证规则
  const rules = {
    store_name: [{
      required: true,
      message: '请输入店铺名称',
      trigger: ['blur', 'change']
    }],
    phone: [{
      required: true,
      message: '请输入联系电话',
      trigger: ['blur', 'change']
    }, {
      // 自定义验证函数
      validator: (rule, value, callback) => {
        // 返回true表示校验通过，返回false表示不通过
        return isMobile(value)
      },
      message: '手机号码不正确',
      // 触发器可以同时用blur和change
      trigger: ['blur'],
    }],
    address: [{
      required: true,
      message: '请输入详细地址',
      trigger: ['blur', 'change']
    }],
  }

  export default {
    data() {
      return {
        form: {
          store_name: '',
		  contact: '',
          phone: '',
          status: 0,
          address: ''
        },
        rules,
        // 加载中
        isLoading: true,
        // 按钮禁用
        disabled: false,
        // 当前收货地址ID
        addressId: null
      }
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad({ addressId }) {
      // 获取当前记录详情
      this.getData()
    },

    // 必须要在onReady生命周期，因为onLoad生命周期组件可能尚未创建完毕
    onReady() {
      this.$refs.uForm.setRules(this.rules)
    },

    methods: {

      // 获取当前记录详情
      getData() {
        const app = this
        SettingApi.getBasic()
          .then(result => {
            const detail = result.data
            app.createFormData(detail)
			console.log(app.form)
          })
      },

      // 生成默认的表单数据
      createFormData(detail) {
        const { form } = this
        form.store_name = detail.store_name
        form.contact = detail.contact
        form.phone = detail.phone
        form.address = detail.address
        form.status = detail.status
      },

      // 表单提交
      handleSubmit() {
        const app = this
        if (app.disabled) {
          return false
        }
        app.$refs.uForm.validate(valid => {
          if (valid) {
            app.disabled = true
            SettingApi.setBasic(app.form)
              .then(result => {
                app.$toast(result.message)
                uni.navigateBack()
              })
              .finally(() => app.disabled = false)
          }
        })
      }

    }
  }
</script>

<style>
  page {
    background: #f7f8fa;
  }
</style>
<style lang="scss" scoped>
  .page-title {
    width: 94%;
    margin: 0 auto;
    padding-top: 40rpx;
    font-size: 28rpx;
    color: rgba(69, 90, 100, 0.6);
  }

  .form-wrapper {
    margin: 20rpx auto 20rpx auto;
    padding: 0 40rpx;
    width: 94%;
    box-shadow: 0 1rpx 5rpx 0px rgba(0, 0, 0, 0.05);
    border-radius: 16rpx;
    background: #fff;
  }

  /* 底部操作栏 */

  .footer {
    margin-top: 60rpx;

    .btn-wrapper {
      height: 100%;
      display: flex;
      align-items: center;
      padding: 0 20rpx;
    }

    .btn-item {
      flex: 1;
      font-size: 28rpx;
      height: 100rpx;
      line-height: 100rpx;
      text-align: center;
      color: #fff;
      border-radius: 50rpx;
    }

    .btn-item-main {
      background: linear-gradient(to right, #f9211c, #ff6335);

      // 禁用按钮
      &.disabled {
        background: #ff9779;
      }
    }

  }
</style>
